import { IPSModelObject } from '@ibiz/dynamic-model-api';
import { IContext, IParams } from '../../core';
import { createOverlay } from '../../model-core';
import { GlobalStore } from '../../store';

/**
 * 模型定位控制器
 *
 * @author chitanda
 * @date 2021-05-12 11:05:49
 * @export
 * @class ModelLocationController
 */
export class ModelLocationController {
  /**
   * 全局数据存储
   *
   * @author chitanda
   * @date 2021-05-13 15:05:44
   * @type {GlobalStore}
   */
  readonly store: GlobalStore = GlobalStore.getInstance();

  /**
   * 模型预览视图
   *
   * @author chitanda
   * @date 2021-05-13 15:05:27
   * @protected
   * @type {HTMLIbzModelViewElement}
   */
  protected viewRef: HTMLIbzModelViewElement = null;

  /**
   * 打开模型导航视图
   *
   * @author chitanda
   * @date 2021-05-13 15:05:08
   * @param {IPSModelObject} view
   * @param {IContext} [context]
   * @param {IParams} [params]
   * @return {*}  {Promise<HTMLIbzModelViewElement>}
   */
  async open(view: IPSModelObject, context?: IContext, params?: IParams): Promise<HTMLIbzModelViewElement> {
    if (view) {
      await view.fill(true);
    }
    const data = this.store.data;
    data.model = view || null;
    data.context = context || {};
    data.params = params || {};
    if (!this.viewRef) {
      this.viewRef = await createOverlay('ibz-model-view');
    }
    Object.assign(this.viewRef, { model: view, context });
    await this.viewRef.present();
    // await this.viewRef.changeFillScreen(true);
    return this.viewRef;
  }
}
